﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HNG.LIB.Controls;
using HNG.Portal.BLL;
using HNG.Portal.DAL;
using HNG.Portal.DAL.EntityClasses;
using HNG.Portal.DAL.FactoryClasses;
using HNG.Portal.DAL.HelperClasses;
using SD.LLBLGen.Pro.ORMSupportClasses;
using System.Web.UI.HtmlControls;
using System.Data;
using System.IO;
public partial class AdminPortal_Controls_HeThong_Contact_Info : System.Web.UI.UserControl
{
    TblLienHeManager _TblLienHeManager = new TblLienHeManager();
    public int CusID = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            try { CusID = Convert.ToInt32(Request.QueryString["id"].Trim()); }
            catch { CusID = 0; }

            if (CusID == 0) //Add cate
            {
                lblTitle.Text = "Thêm liên hệ";
                Toolbox1.ShowDel = false;
                Toolbox1.ShowRefesh = Toolbox1.ShowBack = false;
            }
            else //sua cate
            {
                lblTitle.Text = "Sửa thông tin liên hệ: " + CusID;
                BindData(CusID);
            }
        }
    }


    //load chi tiet san pham
    protected void BindData(int ID)
    {
        TblLienHeEntity _TblLienHeEntity = _TblLienHeManager.SelectOne(ID);
        if (_TblLienHeEntity == null) { lblthongbao.Text = "<img src='../../Images/icon/Icon_warning.gif' alt='Cảnh báo' /> <span style='font-size:11px;color:red;'>  Không có danh mục này </span>"; return; }
        txtID.Text = ID.ToString();
        hidID.Value = ID.ToString();
        txtNguoiGui.Text = Server.HtmlDecode(_TblLienHeEntity.TenNguoiGui);
        txtTieude.Text = Server.HtmlDecode(_TblLienHeEntity.TieuDe);
        txtEmail.Text = Server.HtmlDecode(_TblLienHeEntity.Email);
        txtPhone.Text = _TblLienHeEntity.DienThoai.ToString();
        txtNgayGui.Text = Convert.ToDateTime(_TblLienHeEntity.NgayGui).ToString("MM/dd/yyyy");
        fckMota.Value = Server.HtmlDecode(_TblLienHeEntity.MoTa);

        // update danh dau da doc
        _TblLienHeEntity = _TblLienHeManager.SelectOne(ID);
        _TblLienHeEntity.DanhDau = false;
        _TblLienHeManager.Update(_TblLienHeEntity);
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        BindToData();
        Response.Redirect(SiteMap.CurrentNode.ParentNode.Url);
    }
    protected void btnDel_Click(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(hidID.Value))
        {
            _TblLienHeManager.Delete(Convert.ToInt32(hidID.Value));
        }
        Response.Redirect(SiteMap.CurrentNode.ParentNode.Url);
    }
    protected void btnApply_Click(object sender, EventArgs e)
    {
        if (txtNguoiGui.Text == "") { lblthongbao.Text = "<img src='../Images/icon/Icon_warning.gif' alt='Cảnh báo' />  <span style='font-size:11px;color:red;'> Nhập tên người gửi !</span>"; txtNguoiGui.Focus(); return; }
        if (txtEmail.Text == "") { lblthongbao.Text = "<img src='../Images/icon/Icon_warning.gif' alt='Cảnh báo' />  <span style='font-size:11px;color:red;'> Nhập Email !</span>"; txtEmail.Focus(); return; }
        BindToData();
        Response.Redirect(ResolveUrl("~/AdminPortal/Pages/ContactEdit.aspx?id=") + hidID.Value);
        //BindData(Convert.ToInt32(hidID.Value));
    }

    private void BindToData()
    {
        TblLienHeEntity _TblLienHeEntity = new TblLienHeEntity();
        if (!string.IsNullOrEmpty(hidID.Value))
            _TblLienHeEntity.Id = Convert.ToInt32(hidID.Value);

        //_TblLienHeEntity.TruyCap = 1;
        _TblLienHeEntity.TenNguoiGui = Server.HtmlEncode(txtNguoiGui.Text);
        _TblLienHeEntity.TieuDe = Server.HtmlEncode(txtTieude.Text);
        _TblLienHeEntity.Email = Server.HtmlEncode(txtEmail.Text);
        _TblLienHeEntity.DienThoai = Server.HtmlEncode(txtPhone.Text);
        _TblLienHeEntity.NgayGui =Convert.ToDateTime(txtNgayGui.Text);
        _TblLienHeEntity.MoTa = Server.HtmlEncode(fckMota.Value);
        _TblLienHeEntity.LangId = HNG.LIB.Localization.LocalizationUtils.GetCurrentLanguage();
        _TblLienHeEntity.DanhDau = true;
        if (!string.IsNullOrEmpty(hidID.Value))
        {
            _TblLienHeManager.Update(_TblLienHeEntity);
        }
        else
        {
            _TblLienHeManager.Insert(_TblLienHeEntity);
            hidID.Value = _TblLienHeEntity.Id.ToString();
        }

    }

    protected void btnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect(SiteMap.CurrentNode.ParentNode.Url);
    }
}